// pages/addbaoxiu/addbaoxiu.js

const {addxuqiu,chaxunxuqiu} =require('../../util/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imglist:[],
   memberId:"",
   baochou:"",
   imgUrls:"",
   title:"",
    
   isSelect:false,//展示类型？
   types:['免费','付费'],
   type:"",//类型

  },
  //点击控制下拉框的展示、隐藏
  select:function(){
    var isSelect = this.data.isSelect
    this.setData({ isSelect:!isSelect})
  },
  //点击下拉框选项，选中并隐藏下拉框
  getType:function(e){
    
    let value = e.currentTarget.dataset.type
    this.setData({
      type:value ,
      isSelect: false,
      classifyId:e.currentTarget.dataset.data.id,
      show:e.currentTarget.dataset.data.chargeType
      
     
    })
  },

  input(e){
   
     this.setData({
      title:e.detail.value
     })
    

  },
  add(){
    if(this.data.imglist.length >= 5)
    {
      wx.showToast({
        title: '最大上传5张照片',
        icon:'none'
      })
      return
    }
    let that=this
    let imgs = this.data.imglist
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album', 'camera'],
      success(res) {
        imgs.push(res.tempFiles[0])
        that.setData({
          imglist:imgs
        })
        wx.uploadFile({
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          url: 'https://anjingbao.anjingbao.net/prod-api/api/common/appUpload',
          success (res){
            
            let jsonS = JSON.parse(res.data)
            
            that.setData({
              imgUrls:that.data.imgUrls+"https://anjingbao.anjingbao.net/prod-api"+ jsonS.fileName + ","
            })
            
            
           
            //do something
          }
        })
      }

    })

  },

 shurubaochou(evt){
   this.setData({
     baochou:evt.detail.value
   })
   
 },
  put(){
   this.setData({
     memberId:wx.getStorageSync('memberId'),
     memberName:wx.getStorageSync('memberName')
   })
    let p ={
     memberId:this.data.memberId,
     memberName:wx.getStorageSync('memberName'),
     amountPrice:this.data.baochou,
     title:this.data.title,
     img:this.data.imgUrls,
     classifyId:this.data.classifyId

    }
       
    addxuqiu(p).then(res=>{
      
      if(res.code==200){
        wx.showModal({
          title: "添加成功",
          icon: 'success',
          duration: 2000
        })
        wx.navigateBack({
          delta: 1
        })
        }
       else{
         wx.showToast({
           title: res.msg,
           icon: 'error',
           duration: 3000
         })
   
       }
    
    
  })
 
},
 
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    
    chaxunxuqiu().then(res=>{
      this.setData({
        list:res.data
      })
      
    })
   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})